package cn.tqfeiyang.springplus.warehouse.controller;

import cn.tqfeiyang.springplus.warehouse.service.WarehouseService;
import cn.tqfeiyang.springplus.warehouse.request.WarehouseDTO;
import cn.tqfeiyang.springplus.warehouse.response.WarehouseVO;
import cn.tqfeiyang.springplus.warehouse.request.WarehouseSTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 仓库前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/warehouse/warehouse")
@Tag(name = "基础/仓库")
public class WarehouseController {

    /**
     * 仓库服务
     */
    @Resource
    private WarehouseService warehouseService;

    /**
     * 查询仓库
     *
     * @param warehouseSTO 查询数据
     * @return 仓库列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询仓库")
    @PreAuthorize("hasAnyAuthority('warehouse:warehouse:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<WarehouseVO>> selectWarehousePage(WarehouseSTO warehouseSTO) {
        return ApiResult.success(warehouseService.selectWarehousePage(warehouseSTO));
    }

    /**
     * 读取仓库
     *
     * @param warehouseId 仓库id
     * @return 仓库信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取仓库")
    @PreAuthorize("hasAnyAuthority('warehouse:warehouse:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<WarehouseVO> selectWarehouseInfo(@RequestParam @EntityId Long warehouseId) {
        return ApiResult.success(warehouseService.selectWarehouseInfo(warehouseId));
    }

    /**
     * 新建仓库
     *
     * @param warehouseDTO 请求数据
     * @return 仓库信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建仓库")
    @PreAuthorize("hasAnyAuthority('warehouse:warehouse:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<WarehouseVO> createWarehouse(@RequestBody @Validated(WhenCreate.class) WarehouseDTO warehouseDTO) {
        Long warehouseId = warehouseService.createWarehouse(warehouseDTO);
        return ApiResult.success(warehouseService.selectWarehouseInfo(warehouseId));
    }

    /**
     * 修改仓库
     *
     * @param warehouseId 仓库id
     * @param warehouseDTO 请求数据
     * @return 仓库信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改仓库")
    @PreAuthorize("hasAnyAuthority('warehouse:warehouse:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<WarehouseVO> updateWarehouse(@RequestParam @EntityId Long warehouseId, @RequestBody @Validated(WhenUpdate.class) WarehouseDTO warehouseDTO) {
        warehouseService.updateWarehouse(warehouseId, warehouseDTO);
        return ApiResult.success(warehouseService.selectWarehouseInfo(warehouseId));
    }

    /**
     * 删除仓库
     *
     * @param warehouseId 仓库id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除仓库")
    @PreAuthorize("hasAnyAuthority('warehouse:warehouse:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteWarehouse(@RequestParam @EntityId Long warehouseId) {
        warehouseService.deleteWarehouse(warehouseId);
        return ApiResult.success();
    }
}
